//
//  MainTabBarController.m
//  iOS-CtrlV
//
//  Created by Wang Li on 12-12-19.
//  Copyright (c) 2012年 Wang Li. All rights reserved.
//

#import "MainTabBarController.h"
#import "AttentionViewController.h"
#import "RecommendViewController.h"
#import "NewViewController.h"
#import "OwnViewController.h"
#import "LoginViewController.h"
#import "AddAlbumViewController.h"
#import "DLCImagePickerController.h"

@interface MainTabBarController ()

@end

@implementation MainTabBarController
@synthesize m_UserDic;
@synthesize m_TabbarCustom;
- (id)init
{
    if (self = [super init]) {
        // Custom initialization
        m_TabbarCustom = [[UIView alloc] initWithFrame:CGRectMake(0, 480-20-44-49, 320, 49)];
        [m_TabbarCustom setBackgroundColor:[UIColor colorWithRed:236/255.f green:238/255.f blue:235/255.f alpha:1]];
        m_TabbarCustom.clipsToBounds =NO;
        self.tabBar.opaque =NO;
        self.tabBar.clipsToBounds =NO;
        _IsFirstIn =YES;
        
        CALayer* tLineLayer = [CALayer layer];
        tLineLayer.frame= CGRectMake(0, 0, 320, 1);
        [tLineLayer setBackgroundColor:[UIColor colorWithRed:153/255.f green:153/255.f blue:153/255.f alpha:1].CGColor];
        [m_TabbarCustom.layer addSublayer:tLineLayer];
        
        
        UIButton* tBarBtt0 = [UIButton buttonWithType:UIButtonTypeCustom];
        [tBarBtt0 setTitle:@"关注" forState:UIControlStateNormal];
        [tBarBtt0 setFont:[UIFont boldSystemFontOfSize:14]];
        [tBarBtt0 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        tBarBtt0.frame =CGRectMake(0, 0, 64, 48);
        tBarBtt0.tag =10;
        
        UIButton* tBarBtt1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [tBarBtt1 setTitle:@"推荐" forState:UIControlStateNormal];
        [tBarBtt1 setFont:[UIFont boldSystemFontOfSize:14]];
        [tBarBtt1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        tBarBtt1.frame =CGRectMake(64, 0, 64, 48);
        tBarBtt1.tag=11;
        
        UIButton* tBarBtt2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [tBarBtt2 setTitle:@"最新" forState:UIControlStateNormal];
        [tBarBtt2 setFont:[UIFont boldSystemFontOfSize:14]];
        [tBarBtt2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        tBarBtt2.frame =CGRectMake(128, 0, 64, 48);
        tBarBtt2.tag =12;
        
        UIButton* tBarBtt3 = [UIButton buttonWithType:UIButtonTypeCustom];
        [tBarBtt3 setTitle:@"我的" forState:UIControlStateNormal];
        [tBarBtt3 setFont:[UIFont boldSystemFontOfSize:14]];
        [tBarBtt3 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        tBarBtt3.frame =CGRectMake(192, 0, 64, 48);
        tBarBtt3.tag =13;
        
        UIButton* tBarBtt4 = [UIButton buttonWithType:UIButtonTypeCustom];
        tBarBtt4.frame =CGRectMake(256+6, -10, 51, 51);
        [tBarBtt4 setImage:[UIImage imageNamed:@"add.png"] forState:UIControlStateNormal];
        tBarBtt4.tag =14;
        
        
        [tBarBtt0 addTarget:self action:@selector(customBttPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tBarBtt1 addTarget:self action:@selector(customBttPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tBarBtt2 addTarget:self action:@selector(customBttPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tBarBtt3 addTarget:self action:@selector(customBttPressed:) forControlEvents:UIControlEventTouchUpInside];
        [tBarBtt4 addTarget:self action:@selector(customBttPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [m_TabbarCustom addSubview:tBarBtt0];
        [m_TabbarCustom addSubview:tBarBtt1];
        [m_TabbarCustom addSubview:tBarBtt2];
        [m_TabbarCustom addSubview:tBarBtt3];
        [m_TabbarCustom addSubview:tBarBtt4];
        
        _WillSeeTag =-1;
    }
    return self;
}

-(void) dealloc
{
    [m_UserDic release];
    [m_TabbarCustom release];
    [m_ImagePicker release];
    [tOwnVC release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

	// Do any additional setup after loading the view.
    AttentionViewController* tAttVC = [[AttentionViewController alloc] init];
    tAttVC.m_NeedFirstDataRequest =NO;
    tAttVC.hidesBottomBarWhenPushed =YES;
    RecommendViewController* tRecomVC = [[RecommendViewController alloc] init];
    tRecomVC.hidesBottomBarWhenPushed =YES;
    NewViewController* tNewVC = [[NewViewController alloc] init];
    tNewVC.hidesBottomBarWhenPushed =YES;
    tOwnVC = [[OwnViewController alloc] init];
    tOwnVC.hidesBottomBarWhenPushed =YES;
    m_ImagePicker = [[DLCImagePickerController alloc] init];
    m_ImagePicker.delegate = self;
    m_ImagePicker.Size = CGSizeMake(640, 450);
    UIViewController* tadd = [[UIViewController alloc] init];
    tadd.hidesBottomBarWhenPushed = YES;
    m_ImagePicker.RootuUIViewController = tadd;
    UINavigationController* tNav0 = [[UINavigationController alloc] initWithRootViewController:tAttVC];
    UINavigationController* tNav1 = [[UINavigationController alloc] initWithRootViewController:tRecomVC];
    UINavigationController* tNav2 = [[UINavigationController alloc] initWithRootViewController:tNewVC];
    UINavigationController* tNav3 = [[UINavigationController alloc] initWithRootViewController:tOwnVC];
    UINavigationController* tNav4 = [[UINavigationController alloc] initWithRootViewController:tadd];
    
    [tAttVC release];
    [tRecomVC release];
    [tNewVC release];
//    [tOwnVC release]; 
    self.viewControllers =[NSArray arrayWithObjects:tNav0,tNav1,tNav2,tNav3, tNav4,nil];
    
    [tNav0 release];
    [tNav1 release];
    [tNav2 release];
    [tNav3 release];
    [tNav4 release];
    self.selectedIndex = 0;

}

//-(void) viewWillAppear:(BOOL)animated
//{
//    for (id subObject in self.tabBar.subviews) {
//        //NSLog(@"%@",[subObject description]);
//        if ([NSStringFromClass([subObject class]) isEqualToString:@"UITabBarButton"]) {
//            [subObject removeFromSuperview];
//        }
//    }
//}
- (void)CreatePicker
{
    tOwnVC.isPLook = NO;
    [m_ImagePicker CreateCamera];
}
-(void) viewDidAppear:(BOOL)animated
{
    if (_IsFirstIn) {
        _IsFirstIn =NO;
        UIButton* tBarBtt1 =(UIButton*)[m_TabbarCustom viewWithTag:11];
        [self customBttPressed:tBarBtt1];
    }
    if (_LoginVC != nil) {
        UINavigationController* tNav =[self.viewControllers objectAtIndex:self.selectedIndex];
        MainTabBarController* tMainTabBar = (MainTabBarController*)tNav.tabBarController;
        tMainTabBar.m_TabbarCustom.hidden =YES;
        [tNav pushViewController:_LoginVC animated:NO];
        [_LoginVC release];
        _LoginVC=nil;
    }
}

-(void) loginOkToShowTag
{
    if (_WillSeeTag > 0) {
        UIButton* tBarBtt =(UIButton*)[m_TabbarCustom viewWithTag:_WillSeeTag];
        [self customBttPressed:tBarBtt];
        _WillSeeTag =-1;
    }
    
}


-(void) registLogin
{
    UIButton* tBarBtt =(UIButton*)[m_TabbarCustom viewWithTag:11];
    self.m_UserDic = nil;
    self.m_TabbarCustom.hidden=NO;
    UINavigationController* tNav = [self.viewControllers objectAtIndex:0];
    AttentionViewController* tAtten = [tNav.viewControllers objectAtIndex:0];
    tAtten.m_HaveRequest =NO;
    tAtten.m_NeedFirstDataRequest =YES;
    
    UINavigationController* tNavc = [self.viewControllers objectAtIndex:1];
    [tNavc popToRootViewControllerAnimated:NO];
    [self customBttPressed:tBarBtt];
}

-(void) customBttPressed:(UIButton*)f_sender
{
    int tType =f_sender.tag-10;
    switch (tType) {
        case 0:
        {
            if (m_UserDic == nil) {
                //需要登录
                _WillSeeTag = f_sender.tag;
                LoginViewController* tLoginVC =[[LoginViewController alloc] init];
                UINavigationController* tNav =[self.viewControllers objectAtIndex:self.selectedIndex];
                tLoginVC.navigationItem.hidesBackButton =YES;
                tNav.hidesBottomBarWhenPushed =YES;
                [tNav pushViewController:tLoginVC animated:YES];
                [tLoginVC release];
                return;
            }
            UINavigationController* tNav =[self.viewControllers objectAtIndex:0];
            AttentionViewController* tAttVC =[tNav.viewControllers objectAtIndex:0];
            tAttVC.m_NeedFirstDataRequest =YES;
            self.selectedIndex = 0;
        }
            break;
        case 1:
            self.selectedIndex = 1;
            break;
        case 2:
            self.selectedIndex = 2;
            break;
        case 3:
        {
            if (m_UserDic == nil) {
                //需要登录
                _WillSeeTag = f_sender.tag;
                LoginViewController* tLoginVC =[[LoginViewController alloc] init];
                tLoginVC.navigationItem.hidesBackButton =YES;
                UINavigationController* tNav =[self.viewControllers objectAtIndex:self.selectedIndex];
                [tNav pushViewController:tLoginVC animated:YES];
                [tLoginVC release];
                return;
            }
            self.selectedIndex = 3;
        }
            break;
        case 4:
        {
            if (m_UserDic == nil) {
                //需要登录
                _WillSeeTag = f_sender.tag;
                LoginViewController* tLoginVC =[[LoginViewController alloc] init];
                tLoginVC.navigationItem.hidesBackButton =YES;
                UINavigationController* tNav =[self.viewControllers objectAtIndex:self.selectedIndex];
                [tNav pushViewController:tLoginVC animated:YES];
                [tLoginVC release];
                return;
            }else{
                [self CreatePicker];
          }
        }
            break;
    }
    [self flashBttColor];  
}

-(void) flashBttColor
{
    UIButton* tBarBtt0 =(UIButton*)[m_TabbarCustom viewWithTag:10];
    UIButton* tBarBtt1 =(UIButton*)[m_TabbarCustom viewWithTag:11];
    UIButton* tBarBtt2 =(UIButton*)[m_TabbarCustom viewWithTag:12];
    UIButton* tBarBtt3 =(UIButton*)[m_TabbarCustom viewWithTag:13];
    
    [tBarBtt0 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [tBarBtt1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [tBarBtt2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [tBarBtt3 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    UIButton* tBlackBtt = (UIButton*)[m_TabbarCustom viewWithTag:10+self.selectedIndex];
    [tBlackBtt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
#pragma -mark
#pragma DLCImagePickerDelegate
- (void)GatimagePickerController:(UIImage *)_image picker:(UIImagePickerController *)_picker
{
    if (_image != nil) {
        NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
        _LoginVC =[[AddAlbumViewController alloc] init];
        _LoginVC.headimage = _image;
        _LoginVC.user_id = [tUserDef stringForKey:@"user_id"];
        _LoginVC.navigationItem.hidesBackButton =YES;

      [_picker dismissModalViewControllerAnimated:NO];
    }
}
//- (void)changAddAlbum:(UIImage*)image
//{
//    NSUserDefaults* tUserDef = [NSUserDefaults standardUserDefaults];
//    AddAlbumViewController* tAddAlbumView =[[AddAlbumViewController alloc] init];
//    tAddAlbumView.headimage = image;
//    tAddAlbumView.user_id = [tUserDef stringForKey:@"user_id"];
//    tAddAlbumView.navigationItem.hidesBackButton =YES;
//    UINavigationController* tNav =[self.viewControllers objectAtIndex:self.selectedIndex];
//    MainTabBarController* tMainTabBar = (MainTabBarController*)tNav.tabBarController;
//    tMainTabBar.m_TabbarCustom.hidden =YES;
//    [tNav pushViewController:tAddAlbumView animated:YES];
//    [tAddAlbumView release];
//}




@end
